package com.xbongbong.paas.handle;

import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.toolbox.help.SaasListDataPermissionHelp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author gaojie
 * @version v1.0
 * @date 2022/8/11 18:40
 * @since v1.0
 */
@Component
public class CompetitorAnalysisDataHandle extends AbstractWebDataAnalysisHandle{

    private static final Logger LOGGER = LoggerFactory.getLogger(CompetitorAnalysisDataHandle.class);

    @Resource
    private SaasListDataPermissionHelp saasListDataPermissionHelp;

    @Override
    public PaasFormDataEsListVO analysisData(FormDataListDTO formDataListDTO) throws XbbException {
        PaasFormDataEsListVO formDataEsListVO;
        try {
            //封装可见人，可见部门筛选条件
            List<List<ConditionsEntityExt>> shouldConditionList = formDataListDTO.getShouldConditions();
            saasListDataPermissionHelp.listDepAndUserDataPermission(shouldConditionList, formDataListDTO.getLoginUser(), XbbRefTypeEnum.COMPETITOR.getCode(), formDataListDTO.getLoginUser().getDepSet());
            //查询es
            formDataEsListVO = getEsList(formDataListDTO);
            if (Objects.equals(formDataListDTO.getFromExport(), BasicConstant.ONE)){
                return formDataEsListVO;
            }
        }catch (XbbException xb){
            throw xb;
        }catch (Exception e){
            LOGGER.error("CompetitorAnalysisDataHandle.analysisData错误，错误原因:",e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return formDataEsListVO;
    }
}
